/**
 * $CD全局变量
 */
import { usePageStore } from '@/admin/store/modele/page'

const pageCD = {}
export default {
  // 设置cd
  _set($CD) {
    const { code } = $CD
    pageCD[code] = $CD
    const pageStore = usePageStore()
    pageStore.setPageInfo(window.$CD.pageInfo)
  },
  // 设置cd-通过code
  _reset(code) {
    if (pageCD[code]) {
      window.$CD = pageCD[code]
      const pageStore = usePageStore()
      pageStore.setPageInfo(window.$CD.pageInfo)
    }
  },
  // 删除cd
  _unset({ code, pcode = '' }) {
    console.log('_unset 执行了', code, pcode)
    delete pageCD[code]
    if (pcode && pageCD[pcode]) {
      // 弹框才会走这部分逻辑
      window.$CD = pageCD[pcode]
      const treeInfo = JSON.parse(JSON.stringify(window.$CD.treeInfo || {}))
      const pageStore = usePageStore()
      pageStore.setPageInfo(window.$CD.pageInfo)
      pageStore.setCurrentTree(treeInfo) // 或 pageStore.currentTree = treeInfo
    }
  }
}
